from django.core.mail import send_mail
from django.shortcuts import render_to_response, get_object_or_404
from django.template.context import RequestContext
from colaboradores.models import Colaborador
from fiduciam.models import Noticias, Quemsomos, Portifolios
from django import forms

def homepage(request):
    portifolio = Portifolios.objects.all().order_by('-publicar','-data')
    noticias = Noticias.objects.all().order_by('-publicar','-data')
    quemsomos = Quemsomos.objects.all().order_by('-publicar','-data')
    return render_to_response('fiduciam/index.html',locals(),context_instance=RequestContext(request))

def noticias(request):
    noticias = Noticias.objects.all().order_by('-publicar','-data')
    return render_to_response('fiduciam/noticias.html',locals(),context_instance=RequestContext(request))

def noticiainterna(request,slug):
    
    noticias = Noticias.objects.get(slug=slug)
    colaborador = Colaborador.objects.get(id = noticias.autor.id)
    ultimas_noticias = Noticias.objects.exclude(slug__exact=slug)
    return render_to_response('fiduciam/sub_noticia.html',locals(),context_instance=RequestContext(request))

def portifolio(request):
    noticias = Noticias.objects.all().order_by('-publicar','-data')
    portifolio = Portifolios.objects.all().order_by('-publicar','-data')
    return render_to_response('fiduciam/portifolio.html',locals(),context_instance=RequestContext(request))

def servicos(request):
    noticias = Noticias.objects.all().order_by('-publicar','-data')
    return render_to_response('fiduciam/servicos.html',locals(),context_instance=RequestContext(request))

def contato(request):
    noticias = Noticias.objects.all().order_by('-publicar','-data')
    return render_to_response('fiduciam/contato.html',locals(),context_instance=RequestContext(request))

def quemsomos(request):
    colaboradores = Colaborador.objects.all().order_by('-publicar','data')
    noticias = Noticias.objects.all().order_by('-publicar','-data')
    quemsomos = Quemsomos.objects.all().order_by('-publicar','-data')
    return render_to_response('fiduciam/quem_somos.html',locals(),context_instance=RequestContext(request))

class FormContato(forms.Form):
    nome = forms.CharField(max_length=50)
    email = forms.EmailField(label = ('E-mail'),required=False)
    telefone = forms.CharField(max_length=10,required = False)
    assunto = forms.CharField(max_length=50)
    mensagem = forms.Field(widget=forms.Textarea)

    def enviar(self):
        titulo = 'Mensagem enviada pelo site'
        destino = 'stw.tecnologia.pi@gmail.com'
        texto = """
        Nome: %(nome)s
        E-mail: %(email)s
        Telefone: %(telefone)s
        Assunto: %(assunto)s
        Mensagem: %(mensagem)s
        """ % self.cleaned_data

        send_mail(
            subject=titulo,
            message=texto,
            from_email=destino,
            recipient_list=[destino],
            )

def contato(request):
    if request.method == 'POST':
        form = FormContato(request.POST)

        if form.is_valid():
            form.enviar()
            mostrar = 'Contato enviado!'
            form = FormContato()
    else:
        form = FormContato()

    return render_to_response(
        'fiduciam/contato.html',
        locals(),
        context_instance=RequestContext(request),
        )